import emitter from './emitter'

if (!window.XtxBridge) {
  window.XtxBridge = {}
}

/**
 * 设置头像
 * @param {String} avatar - base64头像
 * @param {Boolean} save - 是否保存，true 代表保存完毕头像，false 代表预览头像。
 */
window.XtxBridge.previewAvatar = (avatar, save) => {
  emitter.emit('fullter_preview_avatar', { avatar, save })
}

/**
 * 提交用户信息
 */
window.XtxBridge.submitProfile = () => {
  emitter.emit('fullter_submit_profile')
}

/**
 * 注入用户信息
 * nickname: '',
 * birthday: '',
 * gender: '男',
 * countyCode: '',
 * cityCode: '',
 * provinceCode: '',
 * profession: '',
 * fullLocation: ''
 */
// window.XtxBridge.injectProfile = (profile) => {
//   if (typeof profile === 'string') {
//     profile = JSON.parse(profile)
//   }
//   emitter.emit('fullter_inject_profile', profile)
// }

/**
 * 提示
 * @param {Object} config - vant tost 配置
 * https://youzan.github.io/vant/#/zh-CN/toast#options
 */
window.XtxBridge.webToast = (config) => {
  emitter.emit('fullter_web_toast', config)
}

/**
 * 获取网页标题，触发FlutterWebTitle频道，发送网页标题
 */
window.XtxBridge.getTitleByWeb = () => {
  window.setTimeout(() => {
    window.FlutterWebTitle && window.FlutterWebTitle.postMessage(document.title)
  }, 0)
}

/**
 * 设置token
 * @param {String} token - app的token
 */
window.XtxBridge.setToken = (token) => {
  emitter.emit('fullter_set_token', token)
}
